<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use cmf\controller\HomeBaseController;
use think\Db;
use think\Validate;
use app\user\model\UserModel;

class RegisterController extends HomeBaseController
{

    /**
     * 前台用户注册
     */
    public function index()
    {
        $redirect = $this->request->post("redirect");
        if (empty($redirect)) {
            $redirect = $this->request->server('HTTP_REFERER');
        } else {
            $redirect = base64_decode($redirect);
        }
        session('login_http_referer', $redirect);
        if (cmf_is_user_login()) {
            return redirect('user/index/index');
        } else {
            $type = $this->request->param('type', 'person');
            $this->assign('type', $type);
            $provinces = Db::name('provinces')->select();
            $citys = Db::name('cities')->where('provinceid', $provinces[0]['provinceid'])->select();
            $areas = Db::name('areas')->where('cityid', $citys[0]['cityid'])->select();

            $this->assign('provinces', $provinces);
            $this->assign('citys', $citys);
            $this->assign('areas', $areas);
            return $this->fetch(":register");
        }
    }

    /**
     * 前台用户注册提交
     */
    public function doRegister()
    {
        if ($this->request->isPost()) {
            $type = $this->request->param('type', 'person');
            if ($type == 'person') {
                $rules = [
                    'captcha' => 'require',
                    'password' => 'require|min:6|max:32',
                    'mobile' => 'require',
                    'email' => 'require',
                    'name' => 'require',
                    'unit' => 'require',
                    'keshi' => 'require',
                    'unit_address' => 'require',
                    'post_code' => 'require',
                    'unit_phone' => 'require'
                ];

                $isOpenRegistration = cmf_is_open_registration();

                if ($isOpenRegistration) {
                    unset($rules['code']);
                }

                $validate = new Validate($rules);
                $validate->message([
                    'email.require' => '邮箱不能为空',
                    'mobile.require' => '手机不能为空',
                    'name.require' => '姓名不能为空',
                    'unit.require' => '单位不能为空',
                    'keshi.require' => '科室不能为空',
                    'unit_address.require' => '单位地址不能为空',
                    'unit_phone.require' => '单位电话不能为空',
                    'post_code.require' => '邮编不能为空',
                    'password.require' => '密码不能为空',
                    'password.max' => '密码不能超过32个字符',
                    'password.min' => '密码不能小于6个字符',
                    'captcha.require' => '验证码不能为空',
                    ''
                ]);

                $data = $this->request->post();
                if (!$validate->check($data)) {
                    $this->error($validate->getError());
                }
                if (!cmf_captcha_check($data['captcha'])) {
                    $this->error('验证码错误');
                }

                if (!$isOpenRegistration) {
                    $errMsg = cmf_check_verification_code($data['username'], $data['code']);
                    if (!empty($errMsg)) {
                        $this->error($errMsg);
                    }
                }
                $register = new UserModel();

//                if (Validate::is($data['username'], 'email')) {
//                    $user['user_email'] = $data['username'];
                $log = $register->registerPerson($data);
//                } else if (preg_match('/(^(13\d|15[^4\D]|17[013678]|18\d)\d{8})$/', $data['username'])) {
//                    $user['mobile'] = $data['username'];
//                    $log = $register->registerMobile($user);
//                } else {
//                    $log = 2;
//                }
                $sessionLoginHttpReferer = session('login_http_referer');
                $redirect = empty($sessionLoginHttpReferer) ? cmf_get_root() . '/' : $sessionLoginHttpReferer;
                switch ($log) {
                    case 0:
                        $this->success('注册成功', $redirect);
                        break;
                    case 1:
                        $this->error("您的账户已注册过");
                        break;
                    case 2:
                        $this->error("您输入的账号格式错误");
                        break;
                    default :
                        $this->error('未受理的请求');
                }
            } else if ($type == 'company') {
                $rules = [

                    'password' => 'require|min:6|max:32',
                    'mobile' => 'require',
                    'email' => 'require',
                    'province' => 'require',
                    'city' => 'require',
                    'area' => 'require',
                    'unit' => 'require',
                    'bumen' => 'require',
                    'contact_name' => 'require',
                    'tel' => 'require',


                ];

                $isOpenRegistration = cmf_is_open_registration();

                if ($isOpenRegistration) {
                    unset($rules['code']);
                }

                $validate = new Validate($rules);
                $validate->message([
                    'email.require' => '邮箱不能为空',
                    'mobile.require' => '手机不能为空',
                    'contact_name.require' => '联系人姓名不能为空',
                    'unit.require' => '单位名称不能为空',
                    'bumen.require' => '部门不能为空',
                    'unit_address.require' => '单位地址不能为空',
                    'tel.require' => '固定电话不能为空',
                    'province.require' => '省份不能为空',
                    'city.require' => '城市不能为空',
                    'area.max' => '区县不能为空',
                    'password.min' => '密码不能小于6个字符',
                    'password.require' => '密码不能为空',
                    'password.max' => '密码不能超过32个字符',
                ]);

                $data = $this->request->post();
                if (!$validate->check($data)) {
                    $this->error($validate->getError());
                }

                $register = new UserModel();

//                if (Validate::is($data['username'], 'email')) {
//                    $user['user_email'] = $data['username'];
                $log = $register->registerCompany($data);
//                } else if (preg_match('/(^(13\d|15[^4\D]|17[013678]|18\d)\d{8})$/', $data['username'])) {
//                    $user['mobile'] = $data['username'];
//                    $log = $register->registerMobile($user);
//                } else {
//                    $log = 2;
//                }
                $sessionLoginHttpReferer = session('login_http_referer');
//                $redirect = empty($sessionLoginHttpReferer) ? cmf_get_root() . '/' : $sessionLoginHttpReferer;
                switch ($log) {
                    case 0:
                        $this->success('注册成功', url('user/index/index'));
                        break;
                    case 1:
                        $this->error("您的账户已注册过");
                        break;
                    case 2:
                        $this->error("您输入的账号格式错误");
                        break;
                    default :
                        $this->error('未受理的请求');
                }
            }


        } else {
            $this->error("请求错误");
        }

    }
}